package com.isyscore.iios.appdemo.controller;


import com.isyscore.device.common.model.RespDTO;
import com.isyscore.iios.appdemo.model.Customer;
import com.isyscore.iios.appdemo.service.CustomerService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

/**
 * 该案例演示了普通业务功能如何操作数据库
 *
 * @author author
 */
@RestController
@RequestMapping("${app.apiPrefix}/customer")
@RequiredArgsConstructor
public class CustomerController {
    private final CustomerService customerService;

    /**
     * 查询单个客户信息接口
     *
     * @param id 客户id
     * @return 客户信息
     */
    @GetMapping("/{id}")
    public RespDTO<Customer> get(@PathVariable("id") String id) {
        return RespDTO.onSuc(customerService.getCustomer(id));
    }

    /**
     * 保存客户信息接口
     *
     * @param customer 客户信息
     * @return 客户信息
     */
    @PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE)
    public RespDTO<Customer> create(@RequestBody Customer customer) {
        return RespDTO.onSuc(customerService.saveCustomer(customer));
    }

    /**
     * 修改客户信息接口
     *
     * @param customer 客户信息
     * @return 客户信息
     */
    @PutMapping(consumes = MediaType.APPLICATION_JSON_VALUE)
    public RespDTO<Customer> update(@RequestBody Customer customer) {
        return RespDTO.onSuc(customerService.updateCustomer(customer));
    }

    /**
     * 删除单个客户信息接口
     *
     * @param id 客户id
     * @return 客户id
     */
    @DeleteMapping("/{id}")
    public RespDTO<String> delete(@PathVariable("id") String id) {
        return RespDTO.onSuc(customerService.deleteCustomer(id));
    }
}
